 

function assessmentPeaceCtrl($scope, $http, ngDialog) {
  'ngInject';

  this.queryData = {};

  this.assessmentPeaceList = {};

  // 分页
  this.paginationData = {
    pageNum: 0,
    pageSize: 10,
    total: ''
  }

  // 页码变化时调用方法
  $scope.pageChange = () => {

    this.paginationData1 = {
      pageNum: this.paginationData.pageNum-1,
      pageSize: 10,
      total: '',
    }

    $http.post(`${SERVER_CONTEXT}/api/securityActivities/app/querySpecialCheckByBid`,
      angular.extend(this.queryData, this.paginationData1)
        )
      .then((resp) => {
        this.assessmentPeaceList = resp.data.data;
        this.paginationData.total = resp.data.total;
    });
  };

  // 分页查询方法
  this.paginationQuery = () => {
    if (this.paginationData.pageNum) {
      this.paginationData2 = {
        pageNum: this.paginationData.pageNum-1,
        pageSize: 10,
        total: '',
      }
    } else {
      this.paginationData2 = {
        pageNum: 0,
        pageSize: 10,
        total: '',
      }
    }
    $http.post(`${SERVER_CONTEXT}/api/securityActivities/app/querySpecialCheckByBid`,
      angular.extend(this.queryData, this.paginationData2)
        )
      .then((resp) => {
        if(resp.statusText == 'OK'){
          this.assessmentPeaceList = resp.data.data;
          this.paginationData.total = resp.data.total;
        }        
    });

  };

  // status == 3 显示考核得分,其他情况显示进行中
  this.getTotalScore = (param) => {
    switch(param.status) {
      case 3:
        return param.totalScore;
        break;
      default:
        return '进行中';
    }
  }

  // 查询
  this.query = (param) => {
    
    if(param.endTime.$viewValue && param.startTime.$viewValue){
      if (param.startTime.$viewValue < param.endTime.$viewValue) {
        this.paginationQuery();
    } else {
        layer.alert('巡查开始时间应小于结束时间');
      }
    } else {
      this.paginationQuery();
    }

  } 

  // 重置
  this.reset = () => {
    this.queryData = {};
  }

  // 新增内业考核
  this.addAssessment = () => {
    ngDialog.open({
      template: 'addAssessment',
      className: 'ngdialog-theme-default pa-special-check-item-list-theme',
      controllerAs: 'vm',
      scope: $scope,
      controller: function addAssessmentCtrl($scope, $http, paFetchService) {
        'ngInject';

        this.innerCheckItemList = [];

        this.treeActive = false; // 不显示tree

        // 打开tree
        this.openTree = () => {
          this.treeActive = true;
        }

        // 关闭tree
        this.closeTree = () => {
          this.treeActive = false;
        };

        // 考核项 => viewModel
        this.handleDataToViewModel = (data) => {
          let _this = this;
          let isDefaultNodeSelected = false;

          function recursionHandleData(data) {
            data.forEach((item) => {
              item.name = item.checkCategoryName;
              item.open = true;
              itemDetail1 = item;
              if (item.sonsList) {
                item.children = item.sonsList;
                recursionHandleData(item.sonsList);
              }
            });
          }
          recursionHandleData(data);
        };

        // 根据数据渲染ztree
        this.renderZtree = () => {
          let _this = this;
          // click触发事件
          function zTreeOnCheck(event, treeId, treeNode) {
            var checked = treeNode.checked;
            var rootNode = angular.copy(treeNode);
            while (rootNode && rootNode.getParentNode()) {
              rootNode = rootNode.getParentNode();
            }
            var rootNodeName = rootNode.name;

            $scope.$apply(() => {
              if (!treeNode.isParent) {
                checked ? addItem(treeNode) : deleteItem(treeNode);
              } else {
                checked ? recursionAddItem(treeNode) : recursionDelItem(treeNode);
              }
            });

            function addItem(node) {
              if (rootNode.name == '内业') {
                _this.innerCheckItemList.push(node);
              } else if (rootNode.checkCategoryName == '外业') {
                _this.outerCheckItemList.push(node);
              }
            }

            function deleteItem(node) {
              if (rootNode.name == '内业') {
                let index = _this.innerCheckItemList.indexOf(node);
                if (index != -1) {
                  _this.innerCheckItemList.splice(index, 1);
                }
              } else if (rootNode.checkCategoryName == '外业') {
                let index = _this.outerCheckItemList.indexOf(node);
                if (index != -1) {
                  _this.outerCheckItemList.splice(index, 1);
                }
              }
            }

            function recursionAddItem(node) {
              node.children.forEach((item) => {
                if (item.isParent) {
                  recursionAddItem(item);
                } else {
                  addItem(item);
                }
              });
            }

            function recursionDelItem(node) {
              node.children.forEach((item) => {
                if (item.isParent) {
                  recursionDelItem(item);
                } else {
                  deleteItem(item);
                }
              });
            }
          };
          // init jquery-Ztree
          let zTreeObj;
          // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
          let setting = {
            callback: {
              onCheck: zTreeOnCheck,
            },
            check: {
              enable: true,
              chkStyle: "checkbox",
              chkboxType: { "Y": "ps", "N": "ps" }
            },
          };
          // zTree 的数据属性，深入使用请参考 API 文档（zTreeNode 节点数据详解）
          let zNodes = this.categoryList;
          $(document).ready(function(){
            let zTreeObj = $.fn.zTree.init($("#tree"), setting, zNodes);
            /*// 激活默认选中菜单
            let defaultNode = zTreeObj.getNodeByParam('isDefaultNode', true);
            zTreeObj.selectNode(defaultNode);*/
          });
        };

        // 保存新增内业考核
        this.save = (param) => {

          // 修改表单是否提交过状态
          param.$submitted = true;

          // 表单校验
          if (param.$invalid) {
            return false;
          }

          let reqData = this.assessmentList;
          reqData.securityCheckClassifyMap = {};         
          let innerList = [];
          this.innerCheckItemList.forEach((item) => {
            innerList.push({checkClassifyId:item.id});
          }); 

          reqData.securityCheckClassifyMap = {
            inner:innerList,
          }
          reqData.dataSource = '0';

          // 判断是否有考核项目
          if (!innerList[0]) {
            layer.alert('请选择考核项目');
            return false;
          }
          
          $http
              .post(`${SERVER_CONTEXT}/api/securityActivities/app/addSpecialCheckTask`,
                reqData)
              .then((resp) => {
                console.log(resp);
                if (resp.data.success) {
                  layer.msg(resp.data.message);
                  $scope.closeThisDialog();
                  $state.go('specialCheckItemList',{type:'inner',id:resp.data.data.taskId});
                } else {
                  layer.alert(resp.data.message);
                }
              });
        }

        // 初始化弹框页面
        this.initPage = () => {
          let _this = this;
          $http.get(`${SERVER_CONTEXT}/api/safeCheck/safe/select/all`).
              then((data) => {
                if (data.statusText != 'OK') {
                } else {
                  _this.categoryList = [];
                  _this.categoryList.push(data.data[0]);
                  _this.handleDataToViewModel(_this.categoryList);
                  _this.renderZtree();
                }
          });

          // 获得标段信息
          paFetchService
              .getSectionList()
              .then((resp) => {
                this.bidList = resp.data;
              });
        }

        this.initPage();
        
      }
    });
  }

  // 删除平安考核列表 
  this.deleteAssessmentPeace = (id) => {
    $scope.modalTitle = '删除平安考核';
    $scope.modalContent = '确认要删除该信息吗?';
    ngDialog.openConfirm({
      template: 'paConfirm',
      className: 'ngdialog-theme-default pa-confirm-theme',
      scope: $scope,
    }).
      then((value) => {
        $http.delete(`${SERVER_CONTEXT}/api/securityActivities/specialCheck/deleteSpecialCheckById/${id}`).
          then((resp) => {
            if (resp.data.success) {
              layer.msg(resp.data.message);
              $state.reload();
            } else {
              layer.alert(resp.data.message);
            }
          });
      }, (reason) => {});
  }

  // 升序、降序(字段暂时未给)
  this.up = (param,index) => {   
    this.queryData.columeName = param;
    this.queryData.order = index;
    this.paginationQuery();
  }

  this.initPage = () => {
    // 获取巡查列表分页
   // this.paginationQuery();
  }

  // 初始化
  this.initPage();

}


export default assessmentPeaceCtrl;